home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Games / Game Sample Code / ZAM 1.0a13 / GameSource / xqueue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  2.8 KB  |  140 lines  |  [TEXT/KAHL]

  1. /*
  2.     xqueue
  3.     ------
  4.     Hairy  queue manager
  5.     disables interrupts both in enqueue and dequeue
  6.     to prevent list mangling and contention between
  7.     the Time Manager and the application.
  8.     
  9.     This code is dependant upon the implementation
  10.     of the xthing structure, since it only queues them up
  11.     could be changed to queue up anything though, just
  12.     watch those qLink fields.
  13.     
  14.     7:17:22 PM  10/28/92
  15.     By Brigham Stevens
  16.     Apple Computer, Inc.
  17.     Developer Technical Support
  18. */
  19.  
  20. #include "xthing.h"
  21. #include "ZAMProtos.h"
  22.  
  23.  
  24. // uncomment out the line below to disable interrupts when playing in
  25. // the queue.  However, the rest of ZAM does not need this anymore,
  26. // but you might like to do it for kicks or your own uses.
  27. // #define DISABLE_INTERRUPTS 1
  28.  
  29. /* set the processor to disable other interrupts */
  30. /* don't do much while interrupts are disabled */
  31. /* or the whole system will go to hell */
  32. short disableInterrupts(void)
  33. {
  34.     asm {
  35.             move    SR,d0            ; return old IntMask
  36.             ori.w    #0x0700,SR
  37.     }
  38. }
  39.  
  40. /* restore previous interrupt level returned from disable above*/
  41. void enableInterrupts(short level)
  42. {
  43.     asm {
  44.             move.w    level,-(sp)            ; push old mask
  45.             move    (sp)+,SR            ; restore that puppy
  46.     }
  47. }
  48.  
  49. void xPanic(short stackFrameGeneratorRequiredToSeeName)
  50. /*
  51.     Maybe you can make the debugger jump here if you are in a hairy death situation
  52. */
  53. {
  54.     enableInterrupts(0);
  55.     ExitToShell();
  56. }
  57.  
  58. void xInitQueueHeader(xQHdr *queue)
  59. /*
  60.     init one of my queue headers
  61. */
  62. {
  63.     queue->qFlags = 0L;
  64.     queue->qHead = nil;
  65.     queue->qTail = nil;
  66.     queue->qEntries = 0;
  67. }
  68.  
  69. void xEnqueue(xthing *qel, xQHdr *queue)
  70. /*
  71.     enqueue a an xthing task
  72. */
  73. {
  74.     short        oldIntMask;
  75.  
  76.     if(!qel)    return;
  77.     
  78. #ifdef DISABLE_INTERRUPTS    
  79.     oldIntMask = disableInterrupts();
  80. #endif
  81.  
  82.     qel->next =  nil;                /* make sure no bad doggies on the end */
  83.     queue->qEntries++;
  84.     
  85.     if(queue->qTail) {                /* add to the end */
  86.         queue->qTail->next = qel;      /* add to queue */
  87.         queue->qTail = qel;            /* placeholder for end */
  88.     } else {                        /* only item in the queue */
  89.         queue->qHead = queue->qTail = qel;
  90.     }
  91.     
  92. #ifdef DISABLE_INTERRUPTS
  93.     enableInterrupts(oldIntMask);
  94. #endif
  95. }
  96.  
  97. void xDequeue(xthing *qel, xQHdr *queue)
  98. /*
  99.     Remove an xthing task from the queue
  100. */
  101. {
  102.     xthing     *curs;
  103.     short    oldIntMask;
  104.     
  105.     if(!qel)    return;
  106.     
  107. #ifdef DISABLE_INTERRUPTS
  108.     oldIntMask = disableInterrupts();
  109. #endif
  110.     
  111.     if(qel == queue->qHead) {
  112.         /* special case if the one */
  113.         /* to be removed is the first item */
  114.         queue->qHead = qel->next;
  115.         queue->qEntries--;
  116.         qel->next = nil;
  117.         if(queue->qTail == qel) {
  118.             queue->qTail = nil;
  119.         }
  120.     } else {
  121.     
  122.         /* nope, so now search the queue */
  123.         curs = queue->qHead;
  124.         while(curs) {
  125.             if(curs->next == qel) {
  126.                 curs->next = qel->next;
  127.                 queue->qEntries--;
  128.                 return;
  129.             }
  130.             curs = curs->next;
  131.         }
  132.         
  133.     }
  134.  
  135. #ifdef DISABLE_INTERRRUPTS    
  136.     enableInterrupts(oldIntMask);
  137. #endif
  138.  
  139. }
  140.